﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MAT.Controllers;

namespace MAT
{
    public partial class frmAddItem : Form
    {
        private Controllers.ctrEan13 ean13 = null;
        Controllers.ctrAddItem _frmAddItem1 = new Controllers.ctrAddItem();
        public frmAddItem()
        {
            InitializeComponent();
            _frmAddItem1.FrmAddItem1 = this;
            }

        private void frmAddItem1_Load(object sender, EventArgs e)
        {
            _frmAddItem1.frmAddItem1_Load();
        }

        private void ptbAssetImage_MouseClick(object sender, MouseEventArgs e)
        {
            _frmAddItem1.pictureBox1_MouseClick();
        }
        private void CreateEan13()
        {
            int sum = 0;
            int digital = 0;
            string[] a = { "893" };
            Random rand = new Random();
            string st = a[rand.Next(0, a.Length)].ToString();
            string s = st + rand.Next(1000, 10000).ToString() + rand.Next(10000, 100000).ToString();
            //            MessageBox.Show(s);
            for (int i = s.Length; i >= 1; i--)
            {
                digital = Convert.ToInt32(s.Substring(i - 1, 1));
                if (i % 2 == 0)
                {
                    sum += digital * 3;
                }
                else
                {
                    sum += digital * 1;
                }
            }
            int X = sum % 10;
            if (X != 0)
            {
                int check = (10 - (X % 10));
                string barcode1 = st + rand.Next(1000, 10000).ToString() + rand.Next(10000, 100000).ToString() + check.ToString();
                ean13 = new ctrEan13();
                ean13.CountryCode = st;
                ean13.ManufacturerCode = rand.Next(1000, 10000).ToString();
                ean13.ProductCode = rand.Next(10000, 100000).ToString();
                ean13.ChecksumDigit = check.ToString();

            }
            else
            {
                int check = (X % 10) * 0;
                string barcode2 = st + rand.Next(1000, 10000).ToString() + rand.Next(10000, 100000).ToString() + check.ToString();
                //MessageBox.Show("Barcode: " + barcode2);
                //int barcode = Convert.ToInt32(barcode2);
                ean13 = new ctrEan13();
                ean13.CountryCode = st;
                ean13.ManufacturerCode = rand.Next(1000, 10000).ToString();
                ean13.ProductCode = rand.Next(10000, 100000).ToString();
                ean13.ChecksumDigit = check.ToString();
                string tong = this.ean13.CountryCode + this.ean13.ManufacturerCode + this.ean13.ProductCode + this.ean13.ChecksumDigit;
            }


        }

        private void btnDraw_Click(object sender, EventArgs e)
        {
            System.Drawing.Graphics g = this.ptbBarCode.CreateGraphics();
            g.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.SystemColors.Control),
                new Rectangle(0, 0, ptbAssetImage.Width, ptbAssetImage.Height));
            CreateEan13();
            //ean13.Scale = (float)Convert.ToDecimal( cboScale.Items [cboScale.SelectedIndex] );
            ean13.DrawEan13Barcode(g, new System.Drawing.Point(0, 0));
            txtBarcode.Text = ean13.CountryCode + ean13.ManufacturerCode + ean13.ProductCode + ean13.ChecksumDigit;
            g.Dispose();

            Graphics graphic = ptbBarCode.CreateGraphics();
            Bitmap bmp = new Bitmap(ptbBarCode.Width, ptbBarCode.Height);

            graphic = Graphics.FromImage(bmp);
            ptbBarCode.DrawToBitmap(bmp, new Rectangle(0, 0, ptbBarCode.Width, ptbBarCode.Height));
            bmp.Save(@"C:\RA.png", System.Drawing.Imaging.ImageFormat.Png);
            bmp.Dispose();


        }

        private void btnDraw_Click_1(object sender, EventArgs e)
        {
            System.Drawing.Graphics g = this.ptbBarCode.CreateGraphics();
            g.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.SystemColors.Control),
                new Rectangle(0, 0, ptbAssetImage.Width, ptbAssetImage.Height));
            CreateEan13();
            //ean13.Scale = (float)Convert.ToDecimal( cboScale.Items [cboScale.SelectedIndex] );
            ean13.DrawEan13Barcode(g, new System.Drawing.Point(0, 0));
            txtBarcode.Text = ean13.CountryCode + ean13.ManufacturerCode + ean13.ProductCode + ean13.ChecksumDigit;
            g.Dispose();

           
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            _frmAddItem1.btnSave_Click();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            _frmAddItem1.btnClose_Click();
        }

        

        private void dtpAquiredDate_ValueChanged(object sender, EventArgs e)
        {
            _frmAddItem1.dtpAquiredDate_ValueChanged();
        }

        private void ptbAssetImage_MouseClick_1(object sender, MouseEventArgs e)
        {
            _frmAddItem1.ptbAssetImage_MouseClick_1();
        }

        private void btnLoadImage_Click(object sender, EventArgs e)
        {
            _frmAddItem1.btnLoadImage_Click();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            _frmAddItem1.btnClear_Click();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            _frmAddItem1.btnReset_Click();
        }
        /*
        private void btnReset1_Click(object sender, EventArgs e)
        {
            _frmAddItem1.btnReset1_Click();
        }

        private void cmbCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            _frmAddItem1.cmbCategory_SelectedIndexChanged();
        }
        */
        private void cmbRoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            _frmAddItem1.cmbRoom_SelectedIndexChanged();
        }

      

       

      


        

       
        

       
    }
}
